<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>线下实践管理</title>

    <style>
        /* --- Global Layout & Variables (Consistent with other pages) --- */
        :root {
            --sidebar-width: 240px;
            --primary-color: #007bff;
            --success-color: #28a745;
            --danger-color: #dc3545;
            --info-color: #17a2b8;
            --light-gray: #f8f9fa;
            --border-color: #dee2e6;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: #f4f5f7;
            margin: 0;
            color: #333;
        }

        /* --- Core Layout Fix & Main Container --- */
        .main-content {
            margin-left: var(--sidebar-width);
            padding: 30px;
        }

        .container-card {
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            padding: 25px 30px;
        }

        /* --- Page Header & Actions --- */
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
        }
        .page-header h2 {
            margin: 0;
            font-size: 1.8em;
        }

        /* --- Search Form --- */
        .search-form {
            margin-bottom: 25px;
        }
        .search-form input[type="text"] {
            padding: 8px 12px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            font-size: 1em;
            min-width: 250px;
        }

        /* --- Success/Error Messages --- */
        .message {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
            border: 1px solid transparent;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border-color: #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border-color: #f5c6cb;
        }

        /* --- Modern Table Styles --- */
        .table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.95em;
        }
        .table th, .table td {
            padding: 12px 15px;
            text-align: left;
            vertical-align: middle;
            border-bottom: 1px solid var(--border-color);
        }
        .table thead th {
            background-color: var(--light-gray);
            font-weight: 600;
            color: #495057;
        }
        .table tbody tr:nth-child(even) {
            background-color: var(--light-gray);
        }
        .table tbody tr:hover {
            background-color: #e9ecef;
        }

        /* --- Action Buttons in Table --- */
        .table-actions {
            display: flex;
            gap: 8px; /* Space between buttons */
        }
        .table-actions form {
            margin: 0; /* Remove default form margin */
        }

        /* --- Button Styles --- */
        .btn {
            display: inline-block;
            font-weight: 500;
            text-align: center;
            vertical-align: middle;
            cursor: pointer;
            user-select: none;
            background-color: transparent;
            border: 1px solid transparent;
            padding: 8px 15px;
            font-size: 0.9em;
            border-radius: 4px;
            transition: all 0.2s ease-in-out;
            text-decoration: none;
            color: #fff;
        }
        .btn:hover { filter: brightness(90%); }
        .btn-primary { background-color: var(--primary-color); border-color: var(--primary-color); }
        .btn-success { background-color: var(--success-color); border-color: var(--success-color); }
        .btn-danger { background-color: var(--danger-color); border-color: var(--danger-color); }
        .btn-info { background-color: var(--info-color); border-color: var(--info-color); }
        .btn-search { color: #333; background-color: #f8f9fa; border-color: #dee2e6; }

    </style>
</head>
<body>
<div th:replace="~{fragments/header :: header}"></div>

<div class="main-content">
    <div class="container-card">

        <div class="page-header">
            <h2>线下实践管理</h2>
            <a th:href="@{/practice/add}" class="btn btn-success">创建新实践</a>
        </div>

        <div th:if="${successMessage}" class="message success" th:text="${successMessage}"></div>
        <div th:if="${errorMessage}" class="message error" th:text="${errorMessage}"></div>

        <div class="search-form">
            <form th:action="@{/practice/manage}" method="get">
                <input type="text" name="keyword" placeholder="输入实践ID或标题搜索" th:value="${keyword}">
                <button type="submit" class="btn btn-search">搜索</button>
            </form>
        </div>

        <table class="table">
            <thead>
            <tr>
                <th>实践ID</th>
                <th>实践标题</th>
                <th>所属课程</th>
                <th>实践时间</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:if="${#lists.isEmpty(practices)}">
                <td colspan="6" style="text-align: center; padding: 40px 0;">您还没有创建任何线下实践。</td>
            </tr>
            <tr th:each="p : ${practices}">
                <td th:text="${p.id}"></td>
                <td class="practice-title" th:text="${p.title}"></td>
                <td th:text="${p.courseName}"></td>
                <td th:text="${#temporals.format(p.practiceTime, 'yyyy-MM-dd HH:mm')}"></td>
                <td th:text="${p.status}"></td>
                <td class="table-actions">
                    <a th:href="@{/practice/view/{id}(id=${p.id})}" class="btn btn-info">查看</a>
                    <form th:if="${p.status == '进行中'}" class="action-form" th:action="@{/practice/cancel/{id}(id=${p.id})}" method="post">
                        <button type="submit" class="btn btn-danger" data-action="取消">取消</button>
                    </form>
                    <form th:if="${p.status == '进行中'}" class="action-form" th:action="@{/practice/close/{id}(id=${p.id})}" method="post">
                        <button type="submit" class="btn btn-primary" data-action="结束">结束</button>
                    </form>
                </td>
            </tr>
            </tbody>
        </table>

    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Enhanced Confirmation Dialogs
        const actionForms = document.querySelectorAll('.action-form');

        actionForms.forEach(form => {
            form.addEventListener('submit', function(event) {
                const button = form.querySelector('button[type="submit"]');
                const action = button.getAttribute('data-action'); // "取消" or "结束"

                // Find the title from the same table row
                const row = form.closest('tr');
                const titleElement = row.querySelector('.practice-title');
                const title = titleElement ? titleElement.innerText : '这个';

                const message = `您确定要 "${action}" ${title} 实践吗？`;

                if (!confirm(message)) {
                    event.preventDefault(); // Stop form submission if user clicks "Cancel"
                }
            });
        });
    });
</script>

</body>
</html>